package com.example.sunmiv1.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.sunmiv1.R;
import com.example.sunmiv1.activity.member.VipAnalysisActivity;
import com.example.sunmiv1.activity.member.VipLabelManagerActivity;
import com.example.sunmiv1.utils.net.ACache;
import com.example.sunmiv1.utils.net.Common;
import com.example.sunmiv1.utils.net.HttpUtils;
import com.example.sunmiv1.utils.net.LoadingDialog;
import com.example.sunmiv1.utils.net.TextCallBack;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class VipIndexActivity extends Activity {

    @BindView(R.id.title_back)
    ImageView titleBack;
    @BindView(R.id.title_title)
    TextView titleTitle;
    @BindView(R.id.vipIndex_new_tv)
    TextView vipIndexNewTv;
    @BindView(R.id.vipIndex_new_linear)
    LinearLayout vipIndexNewLinear;
    @BindView(R.id.vipIndex_all_tv)
    TextView vipIndexAllTv;
    @BindView(R.id.vipIndex_all_linear)
    LinearLayout vipIndexAllLinear;
    @BindView(R.id.vipIndex_manage)
    LinearLayout vipIndexManage;
    @BindView(R.id.vipIndex_record)
    LinearLayout vipIndexRecord;
    @BindView(R.id.vipIndex_add)
    LinearLayout vipIndexAdd;
    @BindView(R.id.vipIndex_analysis)
    LinearLayout vipIndexAnalysis;
    @BindView(R.id.vipIndex_rechargeList)
    LinearLayout vipIndexRechargeList;
    @BindView(R.id.vipIndex_labelManager)
    LinearLayout vipIndexlabelManager;
    @BindView(R.id.vipIndex_memberAnalysis)
    LinearLayout vipIndexmemberAnalysis;

    private Context context;
    private Intent intent;
    private ACache aCache;
    private LoadingDialog loadingDialog;

//    private Handler handler = new Handler(){
//        @Override
//        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
//            switch (msg.what){
//                case 1:
//
//                    break;
//                case 2:
//                    break;
//            }
//        }
//    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_vip_index);
        context = this;
        aCache = ACache.get(context);
        loadingDialog = new LoadingDialog(context);
        ButterKnife.bind(this);
        titleTitle.setText(R.string.vipIndexTitle);
        if ("1".equals(aCache.getAsString("userType"))) {
            vipIndexmemberAnalysis.setVisibility(View.GONE);
            vipIndexAdd.setVisibility(View.GONE);
            vipIndexlabelManager.setVisibility(View.GONE);
            vipIndexAnalysis.setVisibility(View.GONE);
            vipIndexRechargeList.setVisibility(View.VISIBLE);
        }else if ("3".equals(aCache.getAsString("userType"))){
            vipIndexRechargeList.setVisibility(View.GONE);
        }else {
            vipIndexmemberAnalysis.setVisibility(View.VISIBLE);
            vipIndexAdd.setVisibility(View.VISIBLE);
            vipIndexlabelManager.setVisibility(View.VISIBLE);
            vipIndexAnalysis.setVisibility(View.VISIBLE);
            vipIndexRechargeList.setVisibility(View.VISIBLE);
        }
    }

    @OnClick({R.id.title_back, R.id.vipIndex_new_linear, R.id.vipIndex_all_linear, R.id.vipIndex_manage, R.id.vipIndex_record,
            R.id.vipIndex_add, R.id.vipIndex_analysis, R.id.vipIndex_rechargeList, R.id.vipIndex_labelManager, R.id.vipIndex_memberAnalysis})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
            case R.id.vipIndex_new_linear:
                intent = new Intent(context, VipListActivity.class);
                intent.putExtra("type", "1");
                startActivity(intent);
                break;
            case R.id.vipIndex_all_linear:
                intent = new Intent(context, VipListActivity.class);
                intent.putExtra("type", "3");
                startActivity(intent);
                break;
            case R.id.vipIndex_manage:
                intent = new Intent(context, VipListActivity.class);
                intent.putExtra("type", "3");
                startActivity(intent);
                break;
            case R.id.vipIndex_record:
                intent = new Intent(context, VipRecordActivity.class);
                startActivity(intent);
                break;
            case R.id.vipIndex_add:
                intent = new Intent(context, AddVipActivity.class);
                startActivity(intent);
                break;
            case R.id.vipIndex_analysis:
                intent = new Intent(context, MemberStatisticsActivity.class);
                startActivity(intent);
                break;
            case R.id.vipIndex_rechargeList:
                intent = new Intent(context, RechargeListActivity.class);
                startActivity(intent);
                break;
            case R.id.vipIndex_labelManager:
                intent = new Intent(context, VipLabelManagerActivity.class);
                startActivity(intent);
                break;
            case R.id.vipIndex_memberAnalysis:
                intent = new Intent(context, VipAnalysisActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        getvipIndex();
    }

    private void getvipIndex() {
        loadingDialog.show();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("storeid", aCache.getAsString("storeid"));
            jsonObject.put("merchantid", aCache.getAsString("merchantid"));
            HttpUtils.post(context, Common.GET_VIP_INDEX, jsonObject, new TextCallBack() {
                @Override
                protected void onSuccess(String text) {
                    try {
                        JSONObject json = new JSONObject(text);
                        loadingDialog.dismiss();
//                        Toast.makeText(context, json.getString("msg"), Toast.LENGTH_SHORT).show();
                        vipIndexNewTv.setText(json.getString("daycount"));
                        vipIndexAllTv.setText(json.getString("memcount"));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                protected void onFailure(ResponseException e) {
                    loadingDialog.dismiss();
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}
